//
//@author jltxseo
// Created by junlintianxia on 2018/10/14.
//



#include <tutorial/shaders/shaderR.h>
#include <util/ShaderHelper.h>
#include "TutorialLessonFive.h"
#define  LOG_TAG    "TutorialLessonFive"

TutorialLessonFive::TutorialLessonFive() {

}

TutorialLessonFive::~TutorialLessonFive() {

}


void TutorialLessonFive::create() {
    TutorialLesson::create();
    mShader = new Shader(lessonFiveVertexShader,lessonFiveFragmentShader);
    //1. 设置位置属性顶点属性指针
    mShader->setVertexAttribPointer("aPosition", 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), rectangleVerticesData);
    //2.设置纹理坐标
    mShader->setVertexAttribPointer("vTextureCoordinate",2,GL_FLOAT,GL_FALSE,2 * sizeof(float),textureCoord);
}

void TutorialLessonFive::change(int width, int height) {
    TutorialLesson::change(width, height);
}

void TutorialLessonFive::draw() {
    TutorialLesson::draw();
    mShader->setTexture("uTexture",0,texture);
    mShader->use();
    glDrawElements(GL_TRIANGLES, sizeof(rectangleVerticesIndex) / sizeof(rectangleVerticesIndex[0]),
                   GL_UNSIGNED_INT, rectangleVerticesIndex);
}

void TutorialLessonFive::setBitmapForTexture(JNIEnv *env, jobject bitmap) {
    void *data;
    int width = 0, height = 0;
    ShaderHelper::loadTexturePixelsFromBitmap(&data,&width,&height,bitmap);
    texture = mShader->loadGLTexture(data,width,height);
}
